#include "fsw.h"

int main(int argc, char **argv)
{
  ctrl_str	ctrl;
  double 	t = 0, t_out = 0;
  int 		iskip = 0;
  int 		i,j = 0;
  char		str[80];
  FILE*		fh;

  printf("Reading Input\n");
  fload(argv, &ctrl);
  init_fft(&ctrl);	  

  //compute_billet(&ctrl); 
  /*rhs_one(&ctrl,0);
  fsave("billet.txt",&ctrl);*/
  //euler_step(&ctrl);
  put_zero_mode(&ctrl);
  fh = fopen("harmonics.txt","w");
  fprintf(fh, "# This file has data for verification of the code.\n");
  fprintf(fh, "# 1. Time\t2. T = K + P\t3. K\t4. P\t5. Depth\t6. K from x\n\n");
  //printf("K = %e  P = %e  Depth = %f  Mean level = %f\n", ctrl.k_energy, ctrl.p_energy, ctrl.h_curr, ctrl.mean_level);
  rk4(&ctrl,0.);
  while (t < ctrl.t_max) 
  {
    if (t_out < t+1e-12) {
       t_out = t_out + ctrl.tskip;
       if (iskip != 0){
         printf("evol_rk_%04d.txt at time %f  T = %.8e ", iskip, t_out, ctrl.t_energy);
         printf("K = %e  P = %e  Depth = %.15e  ML = %.15e\n", ctrl.k_energy, ctrl.p_energy, ctrl.h_curr, ctrl.mean_level);
 
         fprintf(fh, "%e\t%.15e\t%e\t",t_out, ctrl.t_energy, ctrl.k_energy);
         fprintf(fh, "%e\t%.15e\t%.15e\n",  ctrl.p_energy, ctrl.h_curr,ctrl.k_energy_x);	
         sprintf(str, "evol_rk_%04d.txt",iskip-1);
         fsave(str,&ctrl);
       }
       iskip++;
    }
    rk4(&ctrl,1.);
    t = t + ctrl.dt;
    put_zero_mode(&ctrl);
  }
  fclose(fh);
}
